import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;


public class FeedBackQueue<T> implements Queue <T>{
	LinkedList<T> [] queues;
	
	@SuppressWarnings("unchecked")
	public FeedBackQueue(int num_queues)             
	{
		queues = (LinkedList<T>[])Array.newInstance(LinkedList.class,num_queues);
		for(int i = 0; i < queues.length; i++) 
		{
			queues[i] = new LinkedList<T>();
		}
	}
	
	@Override
	public boolean add(T e) {
		// TODO Auto-generated method stub
		if(((ProcessNode) e).getTimesRan() >= queues.length)
		{
			System.err.println("This shouldn't have happened");
			return false;
		} else {
			queues[((ProcessNode) e).getTimesRan()].add(e);
			return true;
		}
		
	}

	@Override
	public T element() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean offer(T e) {
		// TODO Auto-generated method stub
		if(((ProcessNode) e).getTimesRan() >= queues.length)
		{
			System.err.println("This shouldn't have happened");
			return false;
		} else {
			queues[((ProcessNode) e).getTimesRan()].add(e);
			return true;
		}
	}

	@Override
	public T peek() {
		int i = 0;
		while(i < queues.length && queues[i].isEmpty()) i++;
		return queues[i].peek();
	}

	@Override
	public T poll() {
		int i = 0;
		while(i < queues.length && queues[i].isEmpty()) i++;
		((ProcessNode) this.peek()).incrementTimesRan();
		return queues[i].poll();
	}

	@Override
	public T remove() {
		int i = 0;
		while(i < queues.length && queues[i].isEmpty()) i++;
		return queues[i].remove();
	}

	@Override
	public boolean addAll(Collection<? extends T> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
		int i = 0;
		while(i < queues.length) 
		{
			if(!queues[i].isEmpty())
			{
				queues[i].clear();
			}
			i++;
		}
		
	}

	@Override
	public boolean contains(Object o) {
		int i = 0;
		boolean found = false;
		while(i < queues.length) 
		{
			if(queues[i].contains(o))
			{
				found = true;
				break;
			}
			i++;
		}
		return found;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		int i = 0;
		boolean empty = true;
		while(i < queues.length) 
		{
			if(!queues[i].isEmpty())
			{
				empty = false;
				break;
			}
			i++;
		}
		return empty;
	}

	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean remove(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int size() {
		int i = 0;
		int total_size = 0;
		while(i < queues.length) 
		{
			total_size = queues[i].size();
			i++;
		}
		return total_size;
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
		return null;
	}
}
